/**
 * @filename RoleAction.java
 * @package com.tianjian.system
 * @description 权限控制页面
 * @author 胡正衛
 * @date 2012-07-15 21:45:37
 * @version v0.1
 */
package com.tianjian.oa.system;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;

import com.tianjian.common.BaseAction;
import com.tianjian.common.Criteria;
import com.tianjian.oa.system.pojos.RoleInfo;
import com.tianjian.oa.system.services.RoleService;
import com.tianjian.util.GeneralUitl;

/**
 * 权限控制页面
 * @author 胡正衛
 */
public class RoleAction extends BaseAction {

	private static final long serialVersionUID = -22669425983992776L;

	@Autowired
	private RoleService roleService;

	private RoleInfo roleInfo;

	/** 用户ID-或-角色ID */
	private String id;

	private int userid;

	/** 角色ID列表 */
	private int[] roleid;

	public RoleAction() {
		super("权限设置");
	}

	/**
	 * 显示起始页
	 */
	 @Override
	public String index() {
		return SUCCESS;
	}

	/* (non-Javadoc)
	 * @see com.tianjian.system.BaseAction#view()
	 */
	@Override
	public String view() {
		return SUCCESS;
	}

	/**
	 * 获取数据
	 * @return
	 */
	 @Override
	public String get() {
		Criteria criteria = new Criteria();
		inversionCriteria(criteria);
		List<Integer> roles = new ArrayList<Integer>();
		for(int i = 0; i < userinfo.getRoles().size(); i++) {
			roles.add(userinfo.getRoles().get(i).getId());
		}
		int roleid = Collections.min(roles);
		if(roleid != 1) {
			criteria.put("sys_other", true);
		}
		int total = roleService.getCount(criteria);
		List<RoleInfo> rows = roleService.getData(criteria);
		this.result.put("total", total);
		this.result.put("rows", rows);
		return JSON;
	}

	/**
	 * 获取用户角色设置的列表
	 * @return
	 */
	public void user() {
		if (exec == null) {
			List<RoleInfo> rows = roleService.getRole(userid, true);
			List<Map<String, Object>> maps = GeneralUitl.EasyTree(rows, "id", "name", null);
			print(JSONArray.fromObject(maps).toString());
		} else {
			int affected = roleService.setRole(userid, roleid);
			print("{\"result\":" + affected + "}");
		}
	}

	/**
	 * 添加
	 * @return
	 */
	 @Override
	public String add() {
		int rst = roleService.add(roleInfo);
		result.put("result", rst);
		return JSON;
	}

	/**
	 * 编辑
	 * @return
	 */
	 @Override
	public String edit() {
		int rst = roleService.edit(roleInfo);
		result.put("result", rst);
		return JSON;
	}

	/**
	 * 删除
	 * @return
	 */
	 @Override
	public String del() {
		int rst = roleService.del(id);
		result.put("result", rst);
		return JSON;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setRoleid(int[] roleid) {
		this.roleid = roleid;
	}

	public void setUserid(int userid) {
		this.userid = userid;
	}

	public RoleInfo getRoleInfo() {
		return roleInfo;
	}

	public void setRoleInfo(RoleInfo roleInfo) {
		this.roleInfo = roleInfo;
	}

}
